﻿using Domain.Config;
using Domain.Event;
using Domain.IRepositorys;
using Domain.Model;
using Domain.Types;
using Infrastructure.AggregateBase;
using Infrastructure.Error;
using Infrastructure.UniOfWorkBase;
using Microsoft.IdentityModel.Logging;
using Microsoft.IdentityModel.Tokens;
using System;
using System.IdentityModel.Tokens.Jwt;
using System.Linq;
using System.Security.Claims;
using System.Text;
using Utility;

namespace Domain.Services
{
    public class UserPropService : IService
    {
        public IUserRepository userRepository { get; set; }
        public IEFUnitOfWork efUnitOfWork { get; set; }
        public WalletService walletService { get; set; }
        public IUserPropBuyLogRepository userPropBuyLogRepository { get; set; }

        public UserPropBuyLogAggregate AddProp(int uid, PropType propType, int num, int srcFee, int zKFee, int dCFee)
        {
            var u = userRepository.GetByKey(uid);

            var p = u.UserProps.FirstOrDefault(i=>i.PropType== propType);

            u.UT = DateTime.Now;
            if (p==null)
            {
                p = new UserProp();
                p.PropType = propType;
            }
            p.RemainCount += num;
            p.TotalCount += num;
            p.UT = DateTime.Now;

            UserPropBuyLogAggregate userPropBuyLogAggregate = new UserPropBuyLogAggregate();

            userPropBuyLogAggregate.CT = userPropBuyLogAggregate.UT = DateTime.Now;
            userPropBuyLogAggregate.PayMoney = zKFee;
            userPropBuyLogAggregate.PropType = propType;
            userPropBuyLogAggregate.Count = num;
            userPropBuyLogAggregate.UserID = uid;

            userPropBuyLogRepository.Insert(userPropBuyLogAggregate);

            return userPropBuyLogAggregate;
        }
    }
}
